﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.Entity;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Estimate;
using Estimate.Common.Estimate.EstimateValue;
using Newtonsoft.Json;

namespace Estimate.Reports
{
   public class UsuallyReport
    {
       StandardReport standardReport;
       CollectionRankReport collectionRankReport;
       ForeSpanType[] wthSpans = new ForeSpanType[] { ForeSpanType.FD00_24, ForeSpanType.FD24_48, ForeSpanType.FD48_72, ForeSpanType.FD72_96};
       ForeSpanType[] tempSpans = new ForeSpanType[] { ForeSpanType.FD00_24, ForeSpanType.FD24_48, ForeSpanType.FD48_72, ForeSpanType.FD72_96, ForeSpanType.FD96_120, ForeSpanType.FD120_144, ForeSpanType.FD144_168 };
      
       ForeSendpointType[] sendpoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };
       ForeSemiType semitype = ForeSemiType.Auto;

       UsuallyReportResult result = new UsuallyReportResult();

       public UsuallyReport(string connectionString)
       {
           standardReport = new StandardReport(connectionString, DataTableType.city, null,"");
           collectionRankReport = new CollectionRankReport(connectionString, CollectionRankType.area, "");
       }

       public void Clear()
       {
           standardReport.Clear();
           collectionRankReport.Clear();
           result.success = true.ToString();
           result.msg = "";
           result.data = null;
       }

       public void PrepareData(DateTime beginDate, DateTime endDate)
       {
           try
           {
               standardReport.PrepareData(beginDate, endDate, semitype, wthSpans, tempSpans, sendpoints);
               collectionRankReport.PrepareData(beginDate, endDate, semitype, wthSpans, sendpoints);
           }
           catch (Exception ex)
           {
               result.success = false.ToString();
               result.msg = ex.Message;
           }
       }

       /// <summary>
       /// 获取常用报表数据
       /// </summary>
       /// <returns></returns>
       public string GetReportDataString()
       {
           try
           {
               Dictionary<string, object> Data = new Dictionary<string, object>();

               //《标准报表》
               object standard = standardReport.GetReportData().result["data"];
               Data.Add("standard", standard);

               Dictionary<ForeSpanType, object> NormalAccuracy = new Dictionary<ForeSpanType, object>();
               Dictionary<ForeSpanType, object> NormalSkill = new Dictionary<ForeSpanType, object>();
               Dictionary<ForeSpanType, object> PcAccuracy = new Dictionary<ForeSpanType, object>();
               Dictionary<ForeSpanType, object> PcSkill = new Dictionary<ForeSpanType, object>();
               Dictionary<ForeSpanType, object> Max2Accuracy = new Dictionary<ForeSpanType, object>();
               Dictionary<ForeSpanType, object> Min2Accuracy = new Dictionary<ForeSpanType, object>();
               Dictionary<ForeSpanType, object> Max2Skill = new Dictionary<ForeSpanType, object>();
               Dictionary<ForeSpanType, object> Min2Skill = new Dictionary<ForeSpanType, object>();

               foreach (ForeSpanType span in wthSpans)
               {
                   List<UsuallyReport_CityScore> normal_accuracy = new List<UsuallyReport_CityScore>();
                   List<UsuallyReport_CityScore> normal_skill = new List<UsuallyReport_CityScore>();
                   List<UsuallyReport_CityScore> pc_accuracy = new List<UsuallyReport_CityScore>();
                   List<UsuallyReport_CityScore> pc_skill = new List<UsuallyReport_CityScore>();
                   List<UsuallyReport_CityScore> max2_accuracy = new List<UsuallyReport_CityScore>();
                   List<UsuallyReport_CityScore> min2_accuracy = new List<UsuallyReport_CityScore>();
                   List<UsuallyReport_CityScore> max2_skill = new List<UsuallyReport_CityScore>();
                   List<UsuallyReport_CityScore> min2_skill = new List<UsuallyReport_CityScore>();

                   foreach (KeyValuePair<string, string> city in collectionRankReport.CityCodes)
                   {

                       WaterScore memberWaterScore = collectionRankReport.MemberReportScore.WaterfallLevelWaterScore[city.Value][WaterfallLevel.Normal][span];
                       WaterScore standardWaterScore = collectionRankReport.StandardReportScore.WaterfallLevelWaterScore[city.Value][WaterfallLevel.Normal][span];
                       //《一般性降水准确率》
                       normal_accuracy.Add(new UsuallyReport_CityScore(city.Key, memberWaterScore.TS, 0));
                       //《一般性降水技巧》
                       float skill = GlobalFunction.CalculateWaterSkill(memberWaterScore.TS, standardWaterScore.TS);
                       normal_skill.Add(new UsuallyReport_CityScore(city.Key, skill, 0));


                       memberWaterScore = collectionRankReport.MemberReportScore.WaterfallAmountWaterScore[city.Value][span];
                       standardWaterScore = collectionRankReport.StandardReportScore.WaterfallAmountWaterScore[city.Value][span];
                       //《晴雨检验（PC）准确度》
                       pc_accuracy.Add(new UsuallyReport_CityScore(city.Key, memberWaterScore.PC, 0));
                       //《晴雨检验（PC）技巧》
                       skill = GlobalFunction.CalculateWaterSkill(memberWaterScore.PC, standardWaterScore.PC);
                       pc_skill.Add(new UsuallyReport_CityScore(city.Key, skill, 0));


                       TempScore memberTempScore = collectionRankReport.MemberReportScore.MaxTempScore[city.Value][span];
                       TempScore standardTempScore = collectionRankReport.StandardReportScore.MaxTempScore[city.Value][span];
                       //《Max T2℃准确率》
                       max2_accuracy.Add(new UsuallyReport_CityScore(city.Key, memberTempScore.Tt2, 0));
                       //《Max T2℃技巧值》
                       skill = GlobalFunction.CalculateTempSkill(memberTempScore.Tt2, standardTempScore.Tt2);
                       max2_skill.Add(new UsuallyReport_CityScore(city.Key, skill, 0));


                       memberTempScore = collectionRankReport.MemberReportScore.MinTempScore[city.Value][span];
                       standardTempScore = collectionRankReport.StandardReportScore.MinTempScore[city.Value][span];
                       //《Min T 2℃准确率》
                       min2_accuracy.Add(new UsuallyReport_CityScore(city.Key, memberTempScore.Tt2, 0));
                       //《Min T 2℃技巧值》
                       skill = GlobalFunction.CalculateTempSkill(memberTempScore.Tt2, standardTempScore.Tt2);
                       min2_skill.Add(new UsuallyReport_CityScore(city.Key, skill, 0));
                   }

                   //排序
                   normal_accuracy.Sort(new UsuallyReportCompositeScore());
                   normal_skill.Sort(new UsuallyReportCompositeScore());
                   pc_accuracy.Sort(new UsuallyReportCompositeScore());
                   pc_skill.Sort(new UsuallyReportCompositeScore());
                   max2_accuracy.Sort(new UsuallyReportCompositeScore());
                   max2_skill.Sort(new UsuallyReportCompositeScore());
                   min2_accuracy.Sort(new UsuallyReportCompositeScore());
                   min2_skill.Sort(new UsuallyReportCompositeScore());

                   for (int i = 0; i < collectionRankReport.CityCodes.Count; i++)
                   {
                       normal_accuracy[i].position = i + 1;
                       normal_skill[i].position = i + 1;
                       pc_accuracy[i].position = i + 1;
                       pc_skill[i].position = i + 1;
                       max2_accuracy[i].position = i + 1;
                       max2_skill[i].position = i + 1;
                       min2_accuracy[i].position = i + 1;
                       min2_skill[i].position = i + 1;
                   }

                   NormalAccuracy.Add(span, normal_accuracy);
                   NormalSkill.Add(span, normal_skill);
                   PcAccuracy.Add(span, pc_accuracy);
                   PcSkill.Add(span, pc_skill);
                   Max2Accuracy.Add(span, max2_accuracy);
                   Min2Accuracy.Add(span, min2_accuracy);
                   Max2Skill.Add(span, max2_skill);
                   Min2Skill.Add(span, min2_skill);
               }

               Data.Add("normal_accuracy", NormalAccuracy);
               Data.Add("normal_skill", NormalSkill);
               Data.Add("pc_accuracy", PcAccuracy);
               Data.Add("pc_skill", PcSkill);
               Data.Add("max2_accuracy", Max2Accuracy);
               Data.Add("max2_skill", Max2Skill);
               Data.Add("min2_accuracy", Min2Accuracy);
               Data.Add("min2_skill", Min2Skill);


               result.data = Data;
           }
           catch (Exception ex)
           {
               result.success = false.ToString();
               result.msg = ex.Message;
           }
           return JsonConvert.SerializeObject(result);
       }

     
    }

   public class UsuallyReportResult
   {
       public string success = true.ToString();
       public string msg = "";
       public Dictionary<string, object> data;

       public UsuallyReportResult()
       {

       }
   }

   public class UsuallyReport_CityScore
   {
       public string name;
       public float value;
       public int position;

       public UsuallyReport_CityScore()
       { }

       public UsuallyReport_CityScore(string Name,float Value,int Position)
       {
           name = Name;
           value = float.Parse(string.Format("{0:N2}", Value));
           position = Position;
       }

   }
}
